// miniprogram/pages/sendInfo/sendInfo.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userself: false,
    review: false,
    curInfo:{
      address: '',
      dec: '',
      fileids: []
    },
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    const { markerID, state} = options
    this.setData({
      loading: true
    })
    try {
      const { result: infoRes } = await wx.cloud.callFunction({
        name: 'warninfo',
        data: {
          markerID: markerID
        }
      })
      const [ curInfo = {} ] = infoRes.data
      this.setData({
        curInfo: curInfo
      })
      const { result: userRes } = await wx.cloud.callFunction({
        name: 'getuserid'
      })
      if (state === 'view') {
        const userself = userRes.openid === curInfo._openid
        this.setData({
          userself: userself,
          loading: false
        })
      } else {
        const permission = userRes.permission
        this.setData({
          review: permission,
          loading: false
        })
      }
    } catch (err) {
      console.log(err)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 点击图片查看
  showImg: function (e) {
    console.log(e)
    const current = e.currentTarget.dataset.url
    const urls = this.data.curInfo.fileids
    wx.previewImage({
      current: current, // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    })
  },
  // 删除问题
  delete () {
    const mid = this.data.curInfo._id
    console.log(this.data.curInfo._id)
    wx.cloud.callFunction({
      name: 'delwarn',
      data: {
        markerID: mid
      },
      success: () => {
        wx.redirectTo({
          url: '/pages/tipPage/tipPage?page=2'
        })
      },
      fail: err => {
        console.log(err, 'getuserid')
      }
    })
    console.log('del')
  },
  // 审核通过
  changeSuccess () {
    this.changeStatus(1)
  },
  // 审核拒绝
  changeFail () {
    this.changeStatus(2)
  },
  // 审核提交
  changeStatus (e) {
    console.log(this.data.curInfo._id)
    wx.cloud.callFunction({
      name: 'changewarnstatus',
      data: {
        markerID: this.data.curInfo._id,
        reviewSataus: e
      },
      success: () => {
        setTimeout(() => {
          wx.redirectTo({
            url: '/pages/tipPage/tipPage?page=3'
          })
        }, 500)
      },
      fail: err => {
        console.log(err, 'getuserid')
      }
    })
  }
})